package com.amoby.main.action.business;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amoby.base.common.BaseAction;
import com.amoby.base.utils.JacksonBuilder;
import com.amoby.main.entity.BusinessType;
import com.amoby.main.service.BusinessTypeService;
import com.fasterxml.jackson.core.JsonProcessingException;
/**
 * ParamAction Desc：商户分类管理
 * @address http://www.yingmob.com/
 * @author zhaoyy
 * @date 2016年4月11日 上午10:44:37
 */
@Controller
public class BusinessTypeAction extends BaseAction {
    @Autowired
    private BusinessTypeService businessTypeService;

    @ResponseBody
    @RequestMapping(value = "/business_type_parent.html")
    public Object parentType(HttpServletRequest request, ModelMap model) {
        Map<String, Object> result = getResultMap();
        List<BusinessType> parentTypes = businessTypeService.queryParentType();
        result.put("parentTypes", parentTypes);
        result.put(RESULT, true);
        result.put(MESSAGE, "查询成功！");
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/business_type_sub.html")
    public Object parentSub(Long parentId, HttpServletRequest request, ModelMap model) {
        Map<String, Object> result = getResultMap();
        List<BusinessType> parentTypes = businessTypeService.querySubType(parentId);
        result.put("parentTypes", parentTypes);
        result.put(RESULT, true);
        result.put(MESSAGE, "查询成功！");
        return result;
    }

    /**
     * 初始化页面
     * @author Lasing 2016-5-25 下午2:28:58
     * @param model
     * @return
     */
    @RequestMapping(value = "/menus/bustype_mgr.html", method = RequestMethod.GET)
    public String index(ModelMap model) {
        List<Map<String, Object>> list = businessTypeService.getAllBusinessTypes();
        for (Map<String, Object> map : list) {
            map.put("status", 1);
        }
        model.put("businessTypes", list);
        try {
            model.put("businessTypesJson", JacksonBuilder.mapper().writeValueAsString(model.get("businessTypes")));
        } catch (JsonProcessingException e) {
            getLog(this).error(e.getMessage(), e);
        }
        return AD_HTML + "business/business_type_mgr";
    }

    /**
     * 添加分类
     * @author Lasing 2016-5-25 下午2:29:09
     * @param businessType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/busiType/add.html", method = RequestMethod.POST)
    public Object addBusinessType(BusinessType businessType) {
        Map<String, Object> result = getResultMap();
        try {
            if (businessTypeService.insertBusinessType(businessType) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "新增成功！");
                result.put("id", businessType.getId());
                result.put("code", businessType.getCode());
            } else {
                result.put(RESULT, false);
                result.put(MESSAGE, "新增失败！");
            }
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "系统异常处理失败！");
            getLog(this).error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 修改分类
     * @author Lasing 2016-5-25 下午2:29:25
     * @param businessType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/busiType/upd.html", method = RequestMethod.POST)
    public Object updBusinessType(BusinessType businessType) {
        Map<String, Object> result = getResultMap();
        try {
            if (businessType.getId() != null && businessTypeService.updateBusinessType(businessType) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "更新成功！");
                result.put("code", businessType.getCode());
            } else {
                result.put(RESULT, false);
                result.put(MESSAGE, "更新失败！");
            }
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "系统异常处理失败！");
            getLog(this).error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除分类
     * @author Lasing 2016-5-25 下午2:29:33
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/busiType/del.html", method = RequestMethod.POST)
    public Object delBusinessType(Integer id) {
        Map<String, Object> result = getResultMap();
        try {
            if (id != null && businessTypeService.deleteBusinessType(id) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "删除成功！");
            } else {
                result.put(RESULT, false);
                result.put(MESSAGE, "删除失败！");
            }
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "系统异常处理失败！");
            getLog(this).error(e.getMessage(), e);
        }
        return result;
    }
}
